import java.util.LinkedList;

public class Test {

    public static class SingleLinkedList {

        static class ListNode{
            public int val;
            public ListNode next;

            public ListNode(int val) {
                this.val = val;
            }
        }

        public ListNode head;

        public void creatList(){
            ListNode listNode1 = new ListNode(12);
            ListNode listNode2 = new ListNode(23);
            ListNode listNode3 = new ListNode(34);
            ListNode listNode4 = new ListNode(45);
            ListNode listNode5 = new ListNode(56);
            ListNode listNode6 = new ListNode(67);

            listNode1.next = listNode2;
            listNode2.next = listNode3;
            listNode3.next = listNode4;
            listNode4.next = listNode5;
            listNode5.next = listNode6;
            this.head = listNode1;
        }

        public void display(){
            ListNode cur = head;
            while(cur != null){
                System.out.print(cur.val + " ");
                cur = cur.next;
            }
        }

        //头插法
        public void addFirst(int data){
            ListNode node = new ListNode(data);
            node.next = head;
            head = node;
        }

        //尾插法
        public void addLast(int data){
            ListNode lastNode = new ListNode(data);
            if(head == null){
                head = lastNode;
            }
            ListNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = lastNode;
        }

        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            ListNode node = new ListNode(data);
            ListNode cur = head;
            if(index == 0){
                addFirst(data);
            }else if(index == size()){
                addLast(data);
            }
            for (int i = 0; i < index - 1; i++) {
                cur = cur.next;
            }
            node.next = cur.next;
            cur.next = node;
        }

        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            if(head == null){
                return false;
            }
            ListNode cur = head;
            while(cur != null){
                if(cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }

        //删除第一次出现关键字为key的节点
        public void remove(int key){
            if(head == null){
                return ;
            }
            if(head.val == key){
                head = head.next;
                return ;
            }
            ListNode cur = head;
            ListNode curN = cur.next;
            while(cur.next != null){
                if(curN.val == key){
                    cur.next = curN.next;
                    return ;
                }
                cur = curN;
                curN = curN.next;
            }
            System.out.println("没有找到该数");
        }

        //删除所有值为key的节点
        public void removeAllKey(int key){
            if(head == null) return;
            ListNode cur = head.next;
            ListNode prev = head;
            while(cur != null){
                if(cur.val == key){
                    prev.next = cur.next;
                    cur = cur.next;
                }else{
                    prev = cur;
                    cur = cur.next;
                }
            }
            if(head.val == key)head = head.next;
        }

        //得到单链表的长度
        public int size(){
            ListNode cur = head;
            int count = 0;
            while(cur != null){
                cur = cur.next;
                count++;
            }
            return count;
        }

        public void clear(){
            ListNode cur = head;
            while(cur.next != null){
                ListNode curN = cur.next;
                cur.next = null;
                cur = curN;
            }
            head = null;
        }


    }


}
